//
//  RBLongTextView.m
//  CharmZone
//
//  Created by Lan on 2019/1/28.
//  Copyright © 2019年 SummerTea. All rights reserved.
//

#import "RBLongTextView.h"
#import <Masonry.h>
#import "RanTool.h"

@interface RBLongTextView()

@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UILabel *label;

@end

@implementation RBLongTextView

- (void)setText:(NSString *)text
{
    _text = text;
    NSAttributedString *att = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:self.font color:self.textColor string:text lineSpacing:self.lineSpacing];
    }];
    CGSize size = [RanTool sizeWithAttText:att consSize:CGSizeMake(self.consWidth, CGFLOAT_MAX)];
    CGFloat height = size.height;
    CGFloat width = size.width;
    if (height < self.minHeight)
    {
        [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.minHeight);
        }];
    }
    self.label.attributedText = att;
    if (width < self.consWidth)
    {
        self.label.textAlignment = NSTextAlignmentCenter;
    }
    height = MAX(height, self.minHeight);
    height = MIN(height, self.maxHeight);
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
}

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = [UIScrollView new];
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UILabel *)label
{
    if (!_label)
    {
        self.label = ({
            UILabel *view = [UILabel new];
            view.numberOfLines = 0;
            view.textAlignment = NSTextAlignmentLeft;
            [self.scrollView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
                make.width.mas_equalTo(self.consWidth);
            }];
            view;
        });
    }
    return _label;
}

@end
